﻿using System;
using System.XML;
namespace MythtvGadget
{
    internal static class Utilities
    {
        public static DateTime XMLDateTimeParser(string value)
        {
            try
            {
                string[] parts = value.Split('T');

                string[] dateParts = parts[0].Split('-');
                int year = StringToInt(dateParts[0]);
                int month = StringToInt(dateParts[1]);
                int day = StringToInt(dateParts[2]);

                string[] timeParts = parts[1].Split(':');
                int hour = StringToInt(timeParts[0]);
                int minute = StringToInt(timeParts[1]);
                int second = StringToInt(timeParts[2]);

                // WTF!  Javascript dates use 0 = Jan, ....
                DateTime dt = new DateTime(year, month - 1, day, hour, minute, second);
                
                return dt;

            }
            catch (Exception)
            {
                return null;
            }
        }

        private static int StringToInt(string value)
        {
            // For some reason leading zeros really upset int.Parse, 
            // but only after "07" !?
            while (value.StartsWith('0'))
                value = value.Substr(1);

            int n = (int)int.Parse(value);

            return n;
        }
    }
}